<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="主键" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入主键" />
                </el-form-item>
                <el-form-item label="站点id" prop="siteId">
                    <el-input v-model="formData.siteId" placeholder="请输入站点id" />
                </el-form-item>
                <el-form-item label="短信内容" prop="content">
                    <editor v-model="formData.content" :height="500" />
                </el-form-item>
                <el-form-item label="手机号" prop="mobiles">
                    <el-input v-model="formData.mobiles" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="是否发送成功" prop="isSuccess">
                    <el-input v-model="formData.isSuccess" placeholder="请输入是否发送成功" />
                </el-form-item>
                <el-form-item label="是否删除" prop="isDeleted">
                    <el-input v-model="formData.isDeleted" placeholder="请输入是否删除" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  recordEdit, recordAdd, recordDetail } from '@/api/record'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑短信发送记录' : '新增短信发送记录'
})

const formData = reactive({
    id: '',
    siteId: '',
    content: '',
    mobiles: '',
    isSuccess: '',
    isDeleted: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键',
            trigger: ['blur']
        }
    ],
    siteId: [
        {
            required: true,
            message: '请输入站点id',
            trigger: ['blur']
        }
    ],
    content: [
        {
            required: true,
            message: '请输入短信内容',
            trigger: ['blur']
        }
    ],
    mobiles: [
        {
            required: true,
            message: '请输入手机号',
            trigger: ['blur']
        }
    ],
    isSuccess: [
        {
            required: true,
            message: '请输入是否发送成功',
            trigger: ['blur']
        }
    ],
    isDeleted: [
        {
            required: true,
            message: '请输入是否删除',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await recordEdit(data) : await recordAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await recordDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
